Main <- function(m){
    G = MGen(m, -5,-1)   #conductance matrix
    I = VGen(m,-100,100) #injection current vector
    
    #remove the reference point
    G = G[1:(m-1),1:(m-1)]
    I = I[1:(m-1),1] 
    
    #compute the inverse of G, i.e. the resistance matrix
    Z = solve(G) 
    Y = G 
    print(I)   

    Y_Y = Z
    #the nodal voltage vector
    u = Z%*%I
    w = u
    Q = I
    M = matrix(0, (m-1), 1)
    dz = M%*%t(M)%*%solve(Y)
    ####################################################
    while (1)
    { 
          mesg = winDialogString(message = "Please select next operation.1: add a branch; 2: remove a branch; 3: change the conductance of a branch; 4: exit", default = "4")
          M = matrix(0, (m-1), 1)
          if (mesg == 1) 
          #topology changes-----add a branch between endPoints[1] and endPoints[2]
          {
                 endPoints = sample(1:(m-1),2)
                 i = endPoints[1]
                 j = endPoints[2]
                 g = runif(1, 10, 50) #the condutance of the added branch
          } else if (mesg == 2)         
          #topology changes-----remove a branch between two nodes
          {
                 endPoints = sample(1:(m-1),1)
                 i = endPoints[1]
                 #find a neighbor node of node i randomly
                 index = 0
                 cnt = 0;
                 for(k in 1:(m-1))
                 {
                       if (G[i,k] < 0 )
                       {
                           cnt =cnt + 1 
                           index[cnt] = k
                       } 
                 }
                 endPoints = sample(1:cnt,1)
                 j = index[endPoints]
                 g = - G[i,j]
                 
          }else if (mesg == 3)
          
          #topology changes-----change the conductance of a branch between endPoints[1] and endPoints[2]
          {
                 endPoints = sample(1:(m-1),1)
                 i = endPoints[1]
                 #find a neighbor node of node i randomly
                 index = 0
                 cnt = 0
                 for(k in 1:(m-1))
                 {
                       if (G[i,k] < 0 )
                       {
                           cnt =cnt + 1 
                           index[cnt] = k
                       } 
                 }
                 endPoints = sample(1:cnt,1)
                 j = index[endPoints]
                 g = runif(1, -5, 5)
          }else           
          #exit
          {   
                break
          }
          
          M[i] = 1
          M[j] = -1
          #solve the nodal voltage vector using the direct method

          v_v = (diag(m-1)-(diag(m-1)-dz)%*%M%*%solve((1/g)+t(M)%*%solve(Y)%*%M)%*%t(M)%*%solve(G))%*%(diag(m-1)-dz)%*%I

          Y = Y + M%*%g%*%t(M)  #the conductance matrix after topology changes 
          dz = M%*%solve((1/g)+t(M)%*%solve(G)%*%M)%*%t(M)%*%solve(G)
          #print(dz)

          #print(v_v)
          if ((qr(Y)$rank) < (m-1))
          {
               print('the grid is split into two independent sub-grids' )
               break
          }
          v = solve(Y)%*%I    #the nodal voltage vector after topology changes
          x_x = solve(G)%*%v_v 
          #print (v_v)
          print (v-x_x)
          #c_vv=v_v
          

          #the topology change is equivalent to change the injection current at node i and node j
          c = 1/((1/g) +((Z[i,i]-Z[i,j])-(Z[j,i]-Z[j,j])))
          I_i = 0; 
          for(k in 1:(m-1))
          {
                I_i = I_i + Q[k]*(Z[k,i]-Z[k,j]) 
          }
    
          I_i = I_i*c  
          I_j = -I_i
          Q[i] = Q[i] - I_i  ## the injection current at node i after topology change
          Q[j] = Q[j] - I_j  ## the injection current at node j after topology change
          #print (Q-I)

            
          w = Z%*%Q
          #for(k in 1:(m-1))
          #{
           #     w[k] = w[k] - (Z[k,i]*I_i+Z[k,j]*I_j)  
         # } 
          
         # print(w-v) 
    }
}  